package cn.itcast.nems.studentrecord.util;

import cn.hutool.core.collection.CollectionUtil;
import cn.itcast.nems.employee.dao.entity.Employee;
import cn.itcast.nems.studentrecord.dao.condition.ReceivableAdjustmentState;
import cn.itcast.nems.studentrecord.dao.dataobject.ReceivableAdjustmentDO;
import cn.itcast.nems.studentrecord.dao.entity.ReceivableAdjustment;
import cn.itcast.nems.studentrecord.dto.ReceivableAdjustmentDTO;
import cn.itcast.nems.studentrecord.dto.StudentRecordDTO;
import org.springframework.beans.BeanUtils;
import org.springframework.lang.NonNull;
import org.springframework.lang.Nullable;
import org.springframework.util.StringUtils;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Map;
import java.util.Objects;

import static cn.itcast.nems.studentrecord.service.impl.ReceivableAdjustmentServiceImpl.BUSINESS_LINE_CODE;
import static cn.itcast.nems.studentrecord.service.impl.ReceivableAdjustmentServiceImpl.BUSINESS_LINE_NAME;

/**
 * 具体业务凭单流程工具类
 *
 * @author ChangLu.Gao
 * @version v1.0
 * @program nems-server
 * @description
 * @date 2023/11/22 14:10
 */
public class BusinessApprovalFlowServiceUtil {

    /**
     * 用于计算金额转化的分母
     */
    public final static BigDecimal DENOMINATOR = new BigDecimal("100");

    /**
     * 调价凭单参数转化
     *
     * @param recordDO  调价凭单DO参数
     * @param condition 查询数据库的条件
     * @return cn.itcast.nems.studentrecord.dto.ReceivableAdjustmentDTO
     * @author 高昌露
     * @since 2023/11/22 14:14
     */
    public static ReceivableAdjustmentDTO convert(ReceivableAdjustmentDO recordDO, ReceivableAdjustmentDO condition , Map<String, String> dictionaryMap) {
        ReceivableAdjustmentDTO result = new ReceivableAdjustmentDTO();
        BeanUtils.copyProperties(recordDO, result);
        result.setAmount(BigDecimal.valueOf(recordDO.getAmount()).divide(DENOMINATOR, 2, RoundingMode.HALF_UP));
        result.setOriginalAmount(BigDecimal.valueOf(recordDO.getOriginalAmount()).divide(DENOMINATOR, 2, RoundingMode.HALF_UP));
        result.setTargetAmount(BigDecimal.valueOf(recordDO.getTargetAmount()).divide(DENOMINATOR, 2, RoundingMode.HALF_UP));
        result.setStateDesc(ReceivableAdjustmentState.valueOf(recordDO.getState()).getDesc());
        result.setReasonDescription(CollectionUtil.isNotEmpty(dictionaryMap) && StringUtils.hasText(result.getReason() )? dictionaryMap.get(result.getReason()): null);
        result.setCategory(recordDO.getCategory());
        if (CollectionUtil.isNotEmpty(condition.getWaitApprovalJournalIds()) && condition.getWaitApprovalJournalIds().contains(recordDO.getId())) {
            result.setNeedApproval(Boolean.TRUE);
        }
        result.setBusinessLineCode(BUSINESS_LINE_CODE);
        result.setBusinessLineName(BUSINESS_LINE_NAME);
        return result;
    }

    /**
     * 调价凭单参数转化
     *
     * @param studentRecordDTO     学籍详情
     * @param receivableAdjustment 条件单信息
     * @return cn.itcast.nems.studentrecord.dto.ReceivableAdjustmentDTO
     * @author 高昌露
     * @since 2023/11/23 14:46
     */
    public static ReceivableAdjustmentDTO convert(@NonNull StudentRecordDTO studentRecordDTO,
                                                  @Nullable ReceivableAdjustment receivableAdjustment,
                                                  @Nullable Employee creator
    ) {
        ReceivableAdjustmentDTO result = new ReceivableAdjustmentDTO();
        BeanUtils.copyProperties(studentRecordDTO, result, "id");
        result.setStudentName(studentRecordDTO.getName());
        if (null != receivableAdjustment) {
            BeanUtils.copyProperties(receivableAdjustment, result);
        } else {
            result.setStudentRecordId(studentRecordDTO.getId());
        }
        if (null == receivableAdjustment || ReceivableAdjustmentState.canEdit(receivableAdjustment.getState())) {
            result.setOriginalAmount(studentRecordDTO.getActualAmountDue());
            result.setCanAdjustmentAmount(studentRecordDTO.getActualAmountDue());
        } else {
            result.setOriginalAmount(BigDecimal.valueOf(receivableAdjustment.getOriginalAmount()).divide(DENOMINATOR, 2, RoundingMode.HALF_UP));
            result.setCanAdjustmentAmount(BigDecimal.valueOf(receivableAdjustment.getCanAdjustmentAmount()).divide(DENOMINATOR, 2, RoundingMode.HALF_UP));
        }
        if (null != receivableAdjustment) {
            result.setAmount(BigDecimal.valueOf(receivableAdjustment.getAmount()).divide(DENOMINATOR, 2, RoundingMode.HALF_UP));
            result.setTargetAmount(BigDecimal.valueOf(receivableAdjustment.getTargetAmount()).divide(DENOMINATOR, 2, RoundingMode.HALF_UP));
        } else {
            result.setTargetAmount(studentRecordDTO.getActualAmountDue());
        }
        if (null == receivableAdjustment) {
            result.setState(ReceivableAdjustmentState.DRAFT.name());
            result.setStateDesc(ReceivableAdjustmentState.DRAFT.getDesc());
        } else {
            result.setState(receivableAdjustment.getState().name());
            result.setStateDesc(receivableAdjustment.getState().getDesc());
        }
        if (null != creator) {
            result.setCreatedByName(creator.getName());
        }
        return result;
    }
}
